import Vue from 'vue'
import Vuex from 'Vuex'
import { getlink } from '@/api/getlink.js'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        userid: -1,
        username: "",
        address: "丽人科技园C座",
        city: "西安市",
        shopid: 0,
        // 购物车数据
        shopall: [{
            id: 0,
            goodsid: [1, 2, 3],
            nums: [1, 2, 3],
        }, {
            id: 2,
            goodsid: [1, 2, 3],
            nums: [1, 2, 3],
        }],


        shop: [{
            shopname: "多了米披萨(高新店)",
            goodslist: [{
                    "id": 1,
                    "name": "水果什锦披萨",
                    "price": 18.9,
                    "oldprice": 40,
                    "info": "马苏只是,说明文本xxxxx",
                    "type": ["热销", "优惠"]
                },
                {
                    "id": 2,
                    "name": "牛肉什锦披萨",
                    "price": 18.9,
                    "oldprice": 40,
                    "info": "马苏只是,说明文本xxxxx",
                    "type": ["热销", "新店特惠"]
                },
                {
                    "id": 3,
                    "name": "卤肉什锦披萨",
                    "price": 18.9,
                    "oldprice": 40,
                    "info": "马苏只是,说明文本xxxxx",
                    "type": ["特色披萨系列", "新店特惠"]
                }
            ],



        }, {
            shopname: "肯不起",
            goodslist: [{
                    "id": 1,
                    "name": "水果什锦汉堡",
                    "price": 18.9,
                    "oldprice": 40,
                    "info": "马苏只是,说明文本xxxxx",
                    "type": ["热销", "优惠"]
                },
                {
                    "id": 2,
                    "name": "牛肉什锦汉堡",
                    "price": 18.9,
                    "oldprice": 40,
                    "info": "马苏只是,说明文本xxxxx",
                    "type": ["热销", "新店特惠"]
                },
                {
                    "id": 3,
                    "name": "卤肉什锦汉堡",
                    "price": 18.9,
                    "oldprice": 40,
                    "info": "马苏只是,说明文本xxxxx",
                    "type": ["特色披萨系列", "新店特惠"]
                }
            ],



        }]
    },

    mutations: {
        upaddress(state, payload) {
            state.address = payload.newaddress;
        },
        upcity(state, payload) {
            state.city = payload.newcity;
        },
        upid(state, payload) {
            state.shopid = payload.newid;
        },
        upshopname(state, payload) {
            state.shopname = payload.newshopname;
        },
        upgoodslist(state, payload) {
            state.shop[state.shopid].goodslist.push(payload.addshop);
            state.shop[state.shopid].goodsid.push(payload.addshop.id);
            state.shop[state.shopid].goodsid.push(1);
        },
        upuser(state, payload) {
            state.username = payload.username;
        },
        upuserid(state, payload) {
            state.userid = payload.userid;
        }
    },
    actions: {
        getaxio(context, payload) {
            console.log(context);
            getlink(payload.url).then((ok) => {
                console.log(ok.data.data);

            })
        }
    },
    //       shopid: 0,
    // 购物车数据
    // shopall: [{
    //     id: 0,
    //     goodsid: [1, 2, 3],
    //     nums: [1, 2, 3],
    // }, {
    //     id: 2,
    //     goodsid: [1, 2, 3],
    //     nums: [1, 2, 3],
    // }],
    getters: {
        // 里面就一个形参  state代表的就是数据源
        newcity(state) {
            return state.city.slice(0, 3) + (state.city.length > 3 ? ".." : "");
        },
        // pricesum(state) {
        //     state.shopall.forEach((item, index) => {
        //         if (item.id == state.shopid) {
        //             state.shop[].goodslist[index].
        //         }
        //     });
        // }


    }
})